package com.xl98.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xl98.entity.Groups;
import com.xl98.entity.User;
import com.xl98.entity.UserGroups;
import com.xl98.mapper.GroupsMapper;
import com.xl98.mapper.UserGroupsMapper;
import com.xl98.mapper.UserMapper;
import com.xl98.service.GroupsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@Service
public class GroupsServiceImpl extends ServiceImpl<GroupsMapper, Groups> implements GroupsService {
    @Autowired
    private GroupsMapper groupsMapper;
    @Autowired
    private UserGroupsMapper userGroupsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Groups> selectByName(String name) {
        List<Groups> list = groupsMapper.selectByName(name);
        return list;
    }

    @Override
    public Groups selectByID(String id) {
        Groups groups = groupsMapper.selectOneById(id);
        return groups;
    }

    @Override
    public List<Groups> selectByAdminId(String adminId) {
        List<Groups> list = groupsMapper.selectByAdminId(adminId);
        return list;
    }

    @Override
    public List<Groups> selectByAdminIdName(String adminId, String name) {
        List<Groups> list = groupsMapper.selectByAdminIdName(adminId, name);
        return list;
    }

    @Override
    public List<Groups> selectAll() {
        List<Groups> list = groupsMapper.selectAll();
        return list;
    }

    @Override
    public List<Groups> selectByKeyword(String keyword) {
        List<Groups> list = groupsMapper.selectByKeyword("%"+keyword+"%");
        return list;
    }

    @Override
    public boolean insert(Groups groups) {
        if(groupsMapper.insertOne(groups)>0){
            System.out.println(groups.getId());
            UserGroups userGroups = new UserGroups();
            userGroups.setDeleted(0);
            userGroups.setGroupsId(groups.getId());
            userGroups.setGroupNick("群主");
            userGroups.setUserId(groups.getAdminId());
            userGroupsMapper.insert(userGroups);
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Groups groups) {
        if(groupsMapper.updateOne(groups)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(String id) {
        if(groupsMapper.deleteOneById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Groups> selectByUserId(String userId) {
        List<Groups> list = new ArrayList<>();
        QueryWrapper<UserGroups> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserGroups> list1 = userGroupsMapper.selectList(queryWrapper);
        for(UserGroups userGroups: list1){
            list.add(groupsMapper.selectOneById(userGroups.getGroupsId()));
        }
        return list;
    }

    @Override
    public List<User> selectUserById(String id) {
        List<User> list = new ArrayList<>();
        QueryWrapper<UserGroups> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groups_id",id);
        List<UserGroups> list1 = userGroupsMapper.selectList(queryWrapper);
        for(UserGroups userGroups : list1){
            Map<String, String> map = new HashMap<>();
            map.put("id",userGroups.getUserId());
            User user = userMapper.findByString(map);
            user.setPassword(user.getUsername());
            user.setUsername(userGroups.getGroupNick());
            list.add(user);
        }
        return list;
    }
}
